﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;
using HZ.Supervise.Model.View;

namespace HZ.Supervise.Web.Enterprise
{
    public partial class Company_SC : BasePage
    { 
        SuperviseQuery vs = new SuperviseQuery();
        HZ.Supervise.IBLL.IExecuteContext i = HZ.Supervise.BLLFactory.ExecuteContext();
        IBLL_Company ic = HZ.Supervise.BLLFactory.IBLL_Company();
        IBLL_User iu = BLLFactory.BLL_User();
        KeyOperationLogJG kj;
        public Company_SC()
        {
            this.ModuleNo = Module.企业统计分析.ToString();
            this.PageOperateType = OperateType.生产企业汇总统计.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlArea.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;
                //BindData();
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager_CompanyPS_PageChanged(object sender, EventArgs e)
        {
            //AspNetPager_CompanyPS.CurrentPageIndex = 1; 
            BindData();

        }
        protected void gvwInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            vs = Queryby();
            vs.BackPage = "comsc";

            string sessionId = Guid.NewGuid().ToString();
            vs.CompanyID_TB = e.CommandArgument.ToString().Split('|')[0];
            vs.CompanyName_TB = e.CommandArgument.ToString().Split('|')[1];
            Session[sessionId] = vs;
            GridViewRow row = ((Control)e.CommandSource).NamingContainer as GridViewRow;

            if (e.CommandName == "lbnP")
            {
                Response.Redirect("PurchaseAmountDetail.aspx?sesid=" + sessionId);

            }

            if (e.CommandName == "lbnD")
            {
                Response.Redirect("DistributeAmountDetail.aspx?sesid=" + sessionId);
            }
            if (e.CommandName == "lbnW")
            {
                Response.Redirect("WarehouseAmountDetail.aspx?sesid=" + sessionId);
            }
            if (e.CommandName == "lbnS")
            {
                Response.Redirect("SellAmountDetail.aspx?sesid=" + sessionId);
            }
            //if (e.CommandName == "lbnR")
            //{
            //    Response.Redirect("ReturnAmountDetail.aspx?sesid=" + sessionId);

            //}
        }
        /// <summary>
        /// 绑定数据
        /// modify  by张雄  2011.5.4
        /// </summary>
        public void BindData()
        {
            string orderby = this.OrderFileds.SelectedValue + "  " + this.OrderType.SelectedValue;//排序字段与方式
            vs = Queryby();
            int t = 0;
            System.Data.DataTable dt = i.GetStatistics(3, orderby, vs, this.AspNetPager_CompanyPS.CurrentPageIndex, AspNetPager_CompanyPS.PageSize, out t);
            AspNetPager_CompanyPS.RecordCount = t;

            this.gvwPSLInfo.PageSize = AspNetPager_CompanyPS.PageSize;
            this.gvwPSLInfo.DataSource = dt;
            this.gvwPSLInfo.DataBind();
            dt = i.GetStatistics(3, orderby, vs, 1, 0, out t);

            double PurchaseAmountTotal = 0;
            double WareHouseAmountTotal = 0;
            double DistributeAmountTotal = 0;
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                PurchaseAmountTotal += Convert.ToDouble(dt.Rows[j]["PurchaseAmount"]);
                WareHouseAmountTotal += Convert.ToDouble(dt.Rows[j]["WarehouseAmount"]);
                DistributeAmountTotal += Convert.ToDouble(dt.Rows[j]["DistributeAmount"]);

            }
            lblMemo.Text = "采购金额是" + PurchaseAmountTotal.ToString("0.00") + "元 配送金额是" + DistributeAmountTotal.ToString("0.00") + "元 入库金额是" + WareHouseAmountTotal.ToString("0.00") 
                //+ "元 退货金额是" + ReturnAmountTotal.ToString("0.00") 
                + "元";
            AddLog(string.Format("查询生产企业汇总统计数据!"));

        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager_CompanyPS.CurrentPageIndex = 1;
        }
        /// <summary>
        /// 分页    县区
        /// by张雄
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void CompanyPSL_PS_PageChanged(object sender, EventArgs e)
        {
            BindData(); 
        }
         
        /// <summary>
        /// 导出
        /// modify  by  张雄    2011.5.5
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            //SuperviseQuery vs = new SuperviseQuery();
            string orderby = this.OrderFileds.SelectedValue + "  " + this.OrderType.SelectedValue;//排序字段与方式
            vs = Queryby();
            int t = 0;
            System.Data.DataTable dt = i.GetStatistics(3, orderby, vs, 1, 0, out t); 
            AddLog(string.Format("导出生产企业汇总统计数据成功!"));
            ExportFile.Instance.ExportData(dt, gvwPSLInfo, this.Page);
        }
         
        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns></returns>
        private SuperviseQuery Queryby()
        {
            //时间
            vs.Append += string.Format(" 1=1 ");
            DateTime startime;
            DateTime endtime;
            if (!string.IsNullOrEmpty(txtStratTime.Text.Trim()))
            {
                startime = (DateTime)new HZ.Common.ConvertHelper(txtStratTime.Text.Trim()).ToDateTime;//开始时间
                vs.Append += string.Format(" and SubmiTime >= '{0}' ", startime);
            }
            if (!string.IsNullOrEmpty(txtEndTime.Text.Trim()))
            {
                endtime = (DateTime)new HZ.Common.ConvertHelper(txtEndTime.Text.Trim()).ToDateTime;//结束时间
                if (endtime >= Convert.ToDateTime(txtStratTime.Text.Trim()))
                {
                    endtime = endtime.AddMonths(1).AddDays(-1);
                    vs.Append += string.Format(" and SubmiTime <= '{0}' ", endtime);
                }
                else
                {
                    HZ.Web.WebUtil.Alert("截止时间不能小于起始时间");
                }
            }
            vs.AreaID = ddlArea.SelectedAreaID;
            //投标企业名称
            if (!string.IsNullOrEmpty(txtCompanyName_PS.Text))
            {
                vs.CompanyName_TB = this.txtCompanyName_PS.Text.Trim();
            }
            else
            {
                vs.CompanyName_TB = null;
            }
            Model.User u = iu.GetUserByUserID(CurrentAccount.GetUser().UserID);
            if (u.IsJK == true)
            {
                vs.Append += string.Format(" and HospitalID like 'jk%'");
            }
            return vs;
        }

        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="msg"></param>
        private void AddLog(string msg)
        {
            kj = new KeyOperationLogJG();
            kj.OperationLogSerialID = Guid.NewGuid().ToString();
            kj.Operator = CurrentAccount.GetUser().UserID;
            kj.OrgID = CurrentAccount.GetUser().Org.OrgID;
            kj.OperateTime = DateTime.Now;
            kj.OperateMessage = msg;
            iu.AddKeyOperationLog(kj);
        } 
    }
}
